package com.hb.trade.server.project.push.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.center.au.entity.Org;
import com.hb.core.pojo.project.push.entity.PushDockingLog;
import com.hb.trade.server.project.push.service.PushDockingLogService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 公告平台推送数据记录表 前端控制器
 * </p>
 *
 * @author zhongdl
 * @since 2022-02-22
 */
@RestController
public class PushDockingLogController {

    protected final PushDockingLogService pushDockingLogService;

    public PushDockingLogController(PushDockingLogService pushDockingLogService) {
        this.pushDockingLogService = pushDockingLogService;
    }


    @PostMapping("/tradePushDockingLog/save")
    public Message<Boolean> save(@RequestBody PushDockingLog pushDockingLog) {
        boolean save;

        // 先查询是否推送过
        QueryWrapper<PushDockingLog> wrapper = PushDockingLog.wrapper();
        wrapper.eq("business_id", pushDockingLog.getBusinessId());
        wrapper.eq("type", pushDockingLog.getType());

        List<PushDockingLog> list = pushDockingLogService.list(wrapper);
        if (CollUtil.isEmpty(list)) {
            pushDockingLog.buildBaseData(1);
            save = pushDockingLogService.save(pushDockingLog);
        } else {
            pushDockingLog.buildBaseData(2);
            pushDockingLog.setId(list.get(0).getId());
            save = pushDockingLogService.updateById(pushDockingLog);
        }
        return MsgUtil.success(save);
    }

}
